package com.iswust.visual.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liushuai
 * @create 2023-02-21 18:51
 */

public class WeatherCodeUtil {
    private static final Map<String, String> WEATHER_CODE_MAP = new HashMap<>();

    static {
        WEATHER_CODE_MAP.put("晴", "0");
        WEATHER_CODE_MAP.put("轻雾", "1");
        WEATHER_CODE_MAP.put("雾", "1");
        WEATHER_CODE_MAP.put("霾", "2");
        WEATHER_CODE_MAP.put("大风", "3");
        WEATHER_CODE_MAP.put("毛毛雨", "4");
        WEATHER_CODE_MAP.put("雨", "5");
        WEATHER_CODE_MAP.put(",", "|");
    }

    public static String weatherNameToCode(String weatherName) {
        final String[] weatherNames = weatherName.split("，");

        if (weatherNames.length == 1) {
            return WEATHER_CODE_MAP.get(weatherNames[0]) == null ? ""  : WEATHER_CODE_MAP.get(weatherNames[0]) ;
        }
         StringBuilder weatherCodeStr = new StringBuilder();
        for (int i = 0; i < weatherNames.length; i++) {
            String s = WEATHER_CODE_MAP.get(weatherNames[i]);
            if (s == null) {
                continue;
            } else {
                weatherCodeStr.append(s)
                        .append("|");
            }
        }
        return weatherCodeStr.delete(weatherCodeStr.lastIndexOf("|"), weatherCodeStr.lastIndexOf("|") + 1)
                .toString();
    }

}
